package com.pearadmin.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pearadmin.common.nucleus.base.BaseController;
import com.pearadmin.common.nucleus.domain.response.Result;
import com.pearadmin.common.nucleus.domain.response.TableVO;
import com.pearadmin.common.nucleus.domain.response.TreeVO;
import com.pearadmin.system.domain.SysPower;
import com.pearadmin.system.domain.SysRolePower;
import com.pearadmin.system.service.ISysPowerService;
import com.pearadmin.system.service.ISysRolePowerService;
import com.pearadmin.system.service.ISysUserRoleService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("system/power")
public class SysPowerController extends BaseController {

    private String MODULE_PATH = "system/power/";

    @Resource
    private ISysPowerService sysPowerService;
    @Resource
    private ISysUserRoleService sysUserRoleService;
    @Resource
    private ISysRolePowerService sysRolePowerService;

    @GetMapping("main")
    public ModelAndView index(ModelAndView modelAndView){
        modelAndView.setViewName(MODULE_PATH + "main");
        return modelAndView;
    }

    @GetMapping("data")
    public TableVO data(){
        QueryWrapper<SysPower> queryWrapper = new QueryWrapper<>();
        List<SysPower> data = sysPowerService.list(queryWrapper);
        return TreeTable(data);
    }

    @GetMapping("tree")
    public TreeVO tree(){
       List<SysPower> data = sysPowerService.list(null);
       SysPower sysPower = new SysPower();
       sysPower.setId("0");
       sysPower.setName("顶级权限");
       sysPower.setParentId("-1");
       data.add(sysPower);
       return TreeData(data);
    }



    @GetMapping("getPowerByRoleId/{roleId}")
    public TreeVO getPowerByRoleId(@PathVariable String roleId){
        //根据角色编号获取权限信息
        QueryWrapper<SysRolePower> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        List<SysRolePower> list = sysRolePowerService.list(queryWrapper);
        List<SysPower> powers = sysPowerService.list(null);
        for (SysPower sysPower:powers) {
            for (SysRolePower sysRolePower:list) {
                 if(sysPower.getId().equals(sysRolePower.getPowerId())){
                     sysPower.setCheckArr("1");
                 }

            }
        }
        return TreeData(powers);
    }

    @GetMapping("savePowerByRoleId")
    public Result savePowerByRoleId(String powerIds,String roleId){

        QueryWrapper<SysRolePower> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        boolean b = sysRolePowerService.remove(queryWrapper);
        for (String powerId: Arrays.asList(powerIds.split(","))) {
            SysRolePower sysRolePower = new SysRolePower();
            sysRolePower.setPowerId(powerId);
            sysRolePower.setRoleId(roleId);
            sysRolePowerService.save(sysRolePower);

        }
        return success("保存成功");
    }


    @GetMapping("add")
    public ModelAndView add(ModelAndView modelAndView){
        modelAndView.setViewName(MODULE_PATH+"add");
        return modelAndView;
    }

    @PostMapping("save")
    public Result save(@RequestBody SysPower sysPower){
        boolean result = sysPowerService.save(sysPower);
        return choice(result);
    }

    @GetMapping("edit")
    public ModelAndView edit(ModelAndView modelAndView,String id){
        SysPower sysPower = sysPowerService.getById(id);
        modelAndView.addObject("sysPower",sysPower);
        modelAndView.setViewName(MODULE_PATH+"edit");
        return modelAndView;
    }

    @PutMapping("update")
    public Result update(@RequestBody SysPower sysPower){
        boolean result = sysPowerService.updateById(sysPower);
        return choice(result,"保存成功","保存失败");
    }

    @PutMapping("enable/{id}")
    public Result enable(@PathVariable String id){

        SysPower sysPower = new SysPower();
        sysPower.setId(id);
        sysPower.setStatus("0");

        boolean result = sysPowerService.updateById(sysPower);
        return choice(result);
    }

    @PutMapping("disable/{id}")
    public Result disable(@PathVariable String id){
        SysPower sysPower = new SysPower();
        sysPower.setId(id);
        sysPower.setStatus("0");

        boolean result = sysPowerService.updateById(sysPower);
        return choice(result);
    }

    @DeleteMapping("deleteById/{id}")
    public Result deleteById(@PathVariable String id){
        //查询是否存在子级权限
        QueryWrapper<SysPower> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        if(sysPowerService.list(queryWrapper).size()>0){
            return warming("该权限下存在未删除的子级权限");
        }
        //校监用户是否绑定该权限
        QueryWrapper<SysRolePower> queryRole = new QueryWrapper<>();
        queryRole.eq("power_id",id);
        if(sysRolePowerService.list(queryRole).size()>0){
            return warming("已有角色绑定该权限");
        }
        boolean result = sysPowerService.removeById(id);
        return choice(result);
    }

}
